package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import com.itheima.health.vo.LoginParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhangmeng
 * @description
 * @date 2019/9/6
 **/
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    private static final String CURRENT_USER = "CURRENT_USER";
    @Reference
    private UserService userService;

    /**
     * 根据用户名和密码登录
     *
     * @param request
     * @return
     */
    @PostMapping("/login")
    public Result login(HttpServletRequest request, @RequestBody LoginParam param) {
        log.info("[登录]data:{}",param);
        //rpc调用查询用户信息
        User user = userService.findByUsername(param.getUsername());
        //用户不存在或密码不匹配则登录失败
        if (null == user || !user.getPassword().equals(param.getPassword())) {
            log.info("[登录]失败，user:{}",param.getUsername());

            return new Result(false, MessageConst.LOGIN_FAIL);
        }
        //模拟用户登录成功
        log.info("[登录]成功，user:{}",user.getUsername());
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }

    /**
     * 获取当前登录用户名
     *
     * @return
     */
    @GetMapping("/getUsername")
    public Result getUsername() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (null != principal && principal instanceof org.springframework.security.core.userdetails.User) {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) principal;
            return new Result(true, MessageConst.GET_USERNAME_SUCCESS, user.getUsername());
        }
        return new Result(false, MessageConst.GET_USERNAME_FAIL);
    }

    @RequestMapping("/loginSuccess")
    public  Result loginSuccess(){
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }
    @RequestMapping("/loginFail")
    public Result loginFail(){
        return new Result(false,"登录失败");
    }

    /**
     * 新增用户
     * @return
     */
    @RequestMapping("/addUser")
    public Result add(@RequestBody User user, Integer[] roleIds){
        log.info("[新增用户]data:{},roleIds:{}",user,roleIds);
        userService.addUser(user,roleIds);
        return new Result(true,"新增用户成功");
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPageUser")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[用户-分页查询]data:{}", queryPageBean);
        //rpc查询结果
        PageResult pageResult = userService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true,"查询用户成功",pageResult);
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/findByUserId")
    public Result findById(Integer id) {
        log.info("[用户-根据id查询]id:{}", id);
        //rpc 调用业务处理
        User user = userService.findById(id);
        //封装返回值
        return new Result(true,"查询用户成功", user);

    }
    /**
     * 根据用户ID查询关联的角色ID集和
     *
     * @param id
     * @return
     */
    @RequestMapping("/findRoleByUserId")
    public Result findRoleByUserId(Integer id) {
        log.info("[用户-根据用户ID查询关联的角色ID集和]id:{}", id);

        //rpc 调用业务处理
        List<Integer> itemIds = userService.findRoleByUserId(id);
        //封装返回值
        return new Result(true, MessageConst.ACTION_SUCCESS, itemIds);

    }

    /**
     * 编辑用户
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds){
        log.info("[更新用户]data:{},roleIds:{}",user,roleIds);
        userService.edit(user,roleIds);
        return new Result(true,"更新用户成功");
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        log.info("[用户-根据用户id删除]id:{}", id);

        //rpc 调用业务处理
        userService.delete(id);
        //封装返回值
        return new Result(true, "删除用户成功");

    }

}
